package com.degon.hsyl.choosefile;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.degon.hsyl.choosefile.adapter.ChooseFileCallBack;
import com.degon.hsyl.choosefile.adapter.FragmentPagerAdapter;
import com.degon.hsyl.choosefile.bean.FileBean;
import com.degon.hsyl.choosefile.fragment.DocListFragment;
import com.degon.hsyl.choosefile.fragment.PdfListFragment;
import com.degon.hsyl.choosefile.fragment.PptListFragment;
import com.degon.hsyl.choosefile.fragment.XlsListFragment;
import com.degon.hsyl.choosefile.util.DialogUtil;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class ChooseFileActivity extends AppCompatActivity implements ChooseFileCallBack {

    private RelativeLayout backBtn;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private RelativeLayout confirmButton;
    public List<FileBean> chosenFileList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //设置状态栏颜色为蓝色
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().setStatusBarColor(getResources().getColor(R.color.app_theme));

        setContentView(R.layout.activity_choose_file);

        initViews();

        //tab菜单
        List<String> menu = new ArrayList<>();
        menu.add("文档");
        menu.add("表格");
        menu.add("PPT");
        menu.add("PDF");

        //初始化tabLayout
        tabLayout.addTab(tabLayout.newTab().setText(menu.get(0)));
        tabLayout.addTab(tabLayout.newTab().setText(menu.get(1)));
        tabLayout.addTab(tabLayout.newTab().setText(menu.get(2)));
        tabLayout.addTab(tabLayout.newTab().setText(menu.get(3)));

        //设置监听
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        //初始化ViewPager
        List<Fragment> fileFragments = new ArrayList<>();
        fileFragments.add(new DocListFragment(this));
        fileFragments.add(new XlsListFragment(this));
        fileFragments.add(new PptListFragment(this));
        fileFragments.add(new PdfListFragment(this));
        FragmentPagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager(), fileFragments);
        viewPager.setAdapter(adapter);

        //设置联动
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tabLayout.getTabAt(position).select();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                exitLogic(ChooseFileActivity.this, String.valueOf(chosenFileList.size()));
            }
        });

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitLogic(ChooseFileActivity.this, String.valueOf(chosenFileList.size()));
        }
        return false;
    }

    private void initViews() {
        backBtn = findViewById(R.id.back_btn);
        tabLayout = findViewById(R.id.choose_file_page_tabs);
        viewPager = findViewById(R.id.choose_file_page_view_pager);
        confirmButton = findViewById(R.id.confirm_button);
    }

    @Override
    public void addFile(FileBean file) {
        chosenFileList.add(file);
        LogUtils.i(chosenFileList);
    }

    @Override
    public void removeFile(FileBean file) {
        chosenFileList.remove(getRemovedFileIndex(chosenFileList, file));
        LogUtils.i(chosenFileList);
    }

    private int getRemovedFileIndex(@NonNull List<FileBean> fileList, FileBean fileBean) {
        int result = -1;
        for (int i = 0; i < fileList.size(); i++) {
            if (isTwoFileSame(fileList.get(i), fileBean)) {
                result = i;
                break;
            }
        }
        return result;
    }

    private boolean isTwoFileSame(@NonNull FileBean bean1, @NonNull FileBean bean2) {
        return bean1.getType().equals(bean2.getType()) &&
                bean1.getName().equals(bean2.getName()) &&
                bean1.getSize().equals(bean2.getSize()) &&
                bean1.getPath().equals(bean2.getPath());
    }

    /**
     * 退出文件选择页的逻辑
     *
     * @param activity
     * @param chosenFileCount
     */
    private void exitLogic(Activity activity, String chosenFileCount) {
        if (chosenFileList.size() == 0) {
            finish();
        } else {
            DialogUtil.showAlertDialog(activity, chosenFileCount, false, new DialogUtil.AlertDialogBtnClickListener() {
                @Override
                public void clickPositive() {
                    finish();
                }

                @Override
                public void clickNegative() {

                }
            });
        }
    }

}